package com.ftg.learn.day13;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author 布衣
 */
public class SocketClient {

    public static void main(String[] args) throws IOException {

        Socket socket;
        OutputStream outputStream;
        System.out.println("请输入您的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();

        while (true) {
            System.out.println("请输入您要发送的消息:quit为退出");
            String mess = scanner.next();
            if ("quit".equals(mess)) {
                break;
            }

            socket = new Socket("127.0.0.1", 9999);
            outputStream = socket.getOutputStream();

            StringBuilder messBuilder = new StringBuilder();
            messBuilder.append(name).append(":").append(mess);
            outputStream.write(messBuilder.toString().getBytes(StandardCharsets.UTF_8));
            outputStream.close();
            socket.close();
        }


    }

}
